package com.lynch.adment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

	// 定义Context
	private Context context;
	// 图片尺寸
	//private LayoutParams layoutParams = new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	// 定义整型数组 即图片源
	private List<Bitmap> bitmapList = new ArrayList<Bitmap>();
	//int mGalleryItemBackground;

	// 声明ImageAdapter
	public ImageAdapter(Context context, List<Bitmap> bitmapList) {
		this.context = context;
		this.bitmapList = bitmapList;
		//TypedArray typedArray = context.obtainStyledAttributes(R.styleable.Gallery);
		//mGalleryItemBackground = typedArray.getResourceId(
                //R.styleable.Gallery_android_galleryItemBackground, 0); 
	}
	

	// 获取图片的个数
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	// 获取图片在库中的位置
	public Object getItem(int position) {
		return position;
	}

	// 获取图片ID
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		// 创建一个imageView图像
		ImageView imageView = new ImageView(context);
		
//		System.out.println("第一次图片的的宽度："+imageview.getWidth());
//		System.out.println("第一次图片的的高度："+imageview.getHeight());
		// 求余取出当前条目
		int num = position % bitmapList.size();
		// 给ImageView设置资源
		Bitmap bitmap = bitmapList.get(num);
		//layoutParams.width = bitmap.getIntrinsicWidth();
		//layoutParams.height = bitmap.getIntrinsicHeight();
		//imageview.setImageDrawable(drawable);
		imageView.setImageBitmap(bitmap);
//		System.out.println("最后图片的宽度是："+drawable.getIntrinsicWidth());
//		System.out.println("最后图片的宽度是："+drawable.getIntrinsicHeight());
		// 设置布局 图片120*120
		//imageview.setLayoutParams(layoutParams);
//		System.out.println("第二次图片的的宽度："+imageview.getWidth());
//		System.out.println("第二次图片的的高度："+imageview.getHeight());
		// 设置显示比例类型，意思是拉伸高度，然后自动缩放了
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		//imageView.setBackgroundResource(mGalleryItemBackground);
		return imageView;
	}
		
}
